package edu.yctc.weblog.controller.rest.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.TagRestController;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.TagService;
import edu.yctc.weblog.utils.PageQueryUtil;

@RestController
@RequestMapping(URLConstant.S + URLConstant.TAG)
public class TagRestControllerImpl implements TagRestController {

    @Autowired
    private TagService tagService;

    @Override
    @GetMapping(URLConstant.TAG_GET_TAG_LIST)
    public ResultDO<PageDO> getTagList(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        return tagService.getBlogTagPage(new PageQueryUtil(params));
    }

    @Override
    @PostMapping(URLConstant.TAG_SAVE_TAG)
    public ResultDO<Void> saveTag(@RequestParam String name) {
        if (StringUtils.isBlank(name)) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return tagService.saveTag(name);
    }

    @Override
    @PostMapping(URLConstant.TAG_DELETE_TAG)
    public ResultDO<Void> deleteTag(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return tagService.deleteBatch(ids);
    }

}
